var base_url = 'http://localhost/MyMicroBlog/';

function valid_email(email) {
	var patten = new RegExp(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]+$/);
	return patten.test(email);
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {  
		window.onload = function() {
			oldonload();
			func();
		};
	}
}


function setControlState(ctrl,state){
	if(state){
		$(ctrl).removeClass("error");
		$(ctrl).addClass("success");
	}else{
		$(ctrl).removeClass("success");
		$(ctrl).addClass("error");
	}
}

function stringToDateTime(postdate){
  var second = 1000;
  var minutes = second*60;
  var hours = minutes*60;
  var days = hours*24;
  var months = days*30;
  var twomonths = days*365;
  var myDate = new Date(Date.parse(postdate));   
  if (isNaN(myDate))   
  {    
      myDate =new Date(postdate.replace(/-/g, "/"));   
  } 
  var nowtime = new Date();
  var longtime =nowtime.getTime()- myDate.getTime();
  var showtime = 0;
  if( longtime > months*2 )
  {
      return postdate;
  }
  else if (longtime > months)
  {    
      return " a month ago";
  }
  else if (longtime > days*7)
  {    
      return ("a week ago");
  }
  else if (longtime > days)
  {    
      return(Math.floor(longtime/days)+"days ago");
  }
  else if ( longtime > hours)
  {    
      return(Math.floor(longtime/hours)+"hours ago");
  }
  else if (longtime > minutes)
  {    
      return(Math.floor(longtime/minutes)+"minutes ago");
  }
  else if (longtime > second)
  {    
      return(Math.floor(longtime/second)+"seconds ago");
  }else
  {
      return(longtime+" error ");
  }
 }     

